{extend name="Base" /}
{block name="title"}{$title}{/block}
{block name="header"}{:widget('Widget/nHeader')}{/block}
{block name="footer"}{:widget('Widget/nFooter')}{/block}
{block name="body"}
<div class="am-form am-u-lg-6 am-u-lg-centered am-u-md-6 am-u-md-centered am-u-sm-12 am-u-sm-centered" data-am-validator>
	<fieldset>
		<legend>注册</legend>

		{if condition="DISABLED_REG eq 'on'"}
		<div class="am-form-group am-text-center">当前已经关闭自由注册</div>
		{/if}

		<div class="am-form-group am-form-icon">
			<i class="am-icon-user am-icon-fw"></i>
			<input type="text" class="am-form-field" name="username" minlength="2" maxlength="18" placeholder="输入账号（2~18 个字符）" pattern="^.{2,18}$" required/>
		</div>

		<div class="am-form-group am-form-icon">
			<i class="am-icon-envelope am-icon-fw"></i>
			<input type="email" class="am-form-field" name="email" placeholder="输入邮箱" pattern="^\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}$" required/>
		</div>

		<div class="am-form-group am-form-icon">
			<i class="am-icon-lock am-icon-fw"></i>
			<input type="password" class="am-form-field" name="password1" placeholder="输入密码 (6~32 个字符)" pattern="^.{6,32}$" id="doc-vld-pwd-1" required/>
		</div>

		<div class="am-form-group am-form-icon">
			<i class="am-icon-lock am-icon-fw"></i>
			<input type="password" class="am-form-field" name="password2" placeholder="请再次输入密码" data-equal-to="#doc-vld-pwd-1" required/>
		</div>

		<div class="am-form-group am-form-icon">
			<i class="am-icon-user-plus am-icon-fw"></i>
			<input type="text" class="am-form-field" name="invitecode" placeholder="输入邀请码（非必要）" value="{$invitecode}" pattern="^.{21}$"/>
		</div>

		{:token()}

		<button class="am-btn am-btn-secondary am-center submit" type="button" onclick="submit()">提交</button>
	</fieldset>
</div>

<div class="am-modal am-modal-alert" tabindex="-1" id="my-alert">
	<div class="am-modal-dialog">
		<div class="am-modal-hd">错误</div>
		<div class="am-modal-bd">注册失败</div>
		<div class="am-modal-footer">
			<span class="am-modal-btn" data-am-modal-confirm>知道了</span>
		</div>
	</div>
</div>

<script>
	function submit() {
		var data = {
			username: $("input[name=username]").val(),
			email: $("input[name=email]").val(),
			password1: $("input[name=password1]").val(),
			password2: $("input[name=password2]").val(),
			invitecode: $("input[name=invitecode]").val(),
			__token__: $("input[name=__token__]").val()
		};

		$.ajax({
			type: "POST",
			url: "/index/signup/doSignup",
			cache: false,
			data: data,
			dataType: "json",
			success: function (data) {
				if (data.status === 0) {
					if (data.message) {
						$(".am-modal-bd").html(data.message);
					}
					$("#my-alert").modal({
						onConfirm: function () {
							location.href = "/index/signup";
						}
					});
				} else if (data.status === 1) {
					location.href = "/index/login";
				} else if (data.status === 2) {
					$(".am-modal-bd").html(data.message);
					$("#my-alert").modal();
				} else {
					$(".am-modal-bd").html("系统故障，请稍候再试");
					$("#my-alert").modal();
				}
			}
		});
	}

	document.onkeydown = keyDownSearch;
	function keyDownSearch(e) {
		if (!e) e = window.event;
		if ((e.keyCode || e.which) === 13) {
			submit();
		}
	}
</script>
{/block}